//
//  RCMemberTableViewCell.m
//  RocketProject
//
//  Created by Wu Chengwei on 13-6-19.
//  Copyright (c) 2013年 Rocket. All rights reserved.
//

#import "RCMemberTableViewCell.h"
#import "TTTAttributedLabel.h"
#import "NSAttributedString+LVAddition.h"
#import "CommonDefines.h"
#import "UIColor-Expanded.h"
#import "NSString+NSString_Ex_.h"
#import <QuartzCore/QuartzCore.h>

#define kLineImageFile           @"image_line_type3.png"
#define kCardDescImageFile       @"image_member_card_desc.png"
#define kCardUsewayImageFile     @"image_member_card_useWay.png"
#define kCardMakeImageFile       @"image_member_card_card_make.png"
#define kBGImageFile             @"image_discount_cell_bg.png"

#define kImageViewFrame          CGRectMake(14,22,16,11)
#define kTitleLabelFrame         CGRectMake(38,17,200,16)
#define kContentLabelFrame       CGRectMake(38,43,272,100)
#define kContentColor            0xff989797

@interface RCMemberTableViewCell()

@property(nonatomic,retain)UIImageView *iconImageView;
@property(nonatomic,retain)UILabel *titleLabel;
@property(nonatomic,retain)TTTAttributedLabel *contentLabel;
@property(nonatomic,retain)UIImageView *lineImageView;


@end

@implementation RCMemberTableViewCell

@synthesize iconImageView = _iconImageView;
@synthesize titleLabel = _titleLabel;
@synthesize contentLabel = _contentLabel;
@synthesize lineImageView = _lineImageView;

- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        // Initialization code
        [self createPlaceHolderView];
        UIImage *bgImage = [UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:kBGImageFile ofType:nil]];
        self.layer.contents = (id)bgImage.CGImage;
//        self.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:kBGImageFile ofType:nil]]];
        self.selectionStyle = UITableViewCellSelectionStyleNone;
    }
    return self;
}

-(void)dealloc{
    self.titleLabel = nil;
    self.iconImageView = nil;
    self.contentLabel = nil;
    self.lineImageView = nil;
    [super dealloc];
}

-(void)createPlaceHolderView{
    _iconImageView = [[UIImageView alloc]initWithFrame:kImageViewFrame];
    _iconImageView.backgroundColor = [UIColor clearColor];
    [self addSubview:_iconImageView];
    
    _titleLabel = [[UILabel alloc]initWithFrame:kTitleLabelFrame];
    _titleLabel.backgroundColor = [UIColor clearColor];
    _titleLabel.textColor = [UIColor colorWithHex:0xFF505050];
    _titleLabel.font = [UIFont fontWithName:kFontNameLightHeiti size:16];
    [self addSubview:_titleLabel];
    
    _contentLabel = [[TTTAttributedLabel alloc]initWithFrame:kContentLabelFrame];
    _contentLabel.backgroundColor = [UIColor clearColor];
    _contentLabel.textColor = [UIColor colorWithHex:kContentColor];
    _contentLabel.font = [UIFont fontWithName:kFontNameLightHeiti size:12];
    _contentLabel.numberOfLines = 0;
    _contentLabel.leading = 6;
    [self addSubview:_contentLabel];
    
    _lineImageView = [[UIImageView alloc]init];
    _lineImageView.backgroundColor = [UIColor clearColor];
    NSString *filePath = [[[NSBundle mainBundle]resourcePath]stringByAppendingPathComponent:kLineImageFile];
    [_lineImageView setImage:[UIImage imageWithContentsOfFile:filePath]];
    [self addSubview:_lineImageView];
    
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated
{
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

-(void)setContentViewWithData:(RCMemberDetailInfo *)data CellIndex:(NSInteger)index isLastCell:(BOOL)lastCell{
    if (nil == data) {
        return;
    }
    NSString *resourcePath = [[NSBundle mainBundle]resourcePath];
    NSString *imagePath = nil;
    switch (index) {
        case 0:{
            imagePath = [resourcePath stringByAppendingPathComponent:kCardDescImageFile];
            break;
        }
        case 1:{
            imagePath = [resourcePath stringByAppendingPathComponent:kCardUsewayImageFile];
            break;
        }
        case 2:{
            imagePath = [resourcePath stringByAppendingPathComponent:kCardMakeImageFile];
            break;
        }
            
        default:{
            imagePath = [resourcePath stringByAppendingPathComponent:kCardMakeImageFile];
            break;
        }
            
    }
    [_iconImageView setImage:[UIImage imageWithContentsOfFile:imagePath]];
    if (![NSString stringIsNullOrEmpty:data.title]) {
        _titleLabel.text = data.title;
    }
    
    CGFloat cellHeight = 43;
    if (![NSString stringIsNullOrEmpty:data.content]) {
        _contentLabel.text = data.content;
        NSAttributedString *attributeStr = _contentLabel.attributedText;
        CGSize size = [attributeStr sizeWithConstrainedToSize:CGSizeMake(272, 2000) Leading:6];
        cellHeight += size.height +10;
        _contentLabel.frame = CGRectMake(38,43,272, size.height);
    }
    _lineImageView.frame = CGRectMake(0, cellHeight, 320, 1);
    if (lastCell) {
        _lineImageView.hidden = YES;
    }else{
        _lineImageView.hidden = NO;
    }
}


+(CGFloat)getCellHeight:(RCMemberDetailInfo *)data{
    if (nil == data) {
        return 0;
    }
    
    CGFloat cellHeight = 43;
    
    if (![NSString stringIsNullOrEmpty:data.content]){
        TTTAttributedLabel *label = [[TTTAttributedLabel alloc]initWithFrame:kContentLabelFrame];
        label.backgroundColor = [UIColor clearColor];
        label.textColor = [UIColor colorWithHex:kContentColor];
        label.font = [UIFont fontWithName:kFontNameLightHeiti size:12];
        label.numberOfLines = 0;
        label.leading = 6;
        label.text = data.content;
        NSAttributedString *attributeStr = label.attributedText;
        CGSize size = [attributeStr sizeWithConstrainedToSize:CGSizeMake(272, 2000) Leading:6];
        cellHeight += size.height +10;
        [label release];
    }
    cellHeight +=2;
    return cellHeight;
}

@end
